package com.zhuyuan.security.service;

import com.xkcoding.http.util.StringUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @Author: 张琳凯
 * @Description: 自定义权限校验工具类
 * @DateTime: 2025/2/28 13:42
 **/
@Component("auth")
public class PermissionService {

    public boolean hasPermi(String permission) {
        if (StringUtil.isEmpty(permission)){
            return false;
        }
        // 从SecurityContext中获取用户权限并匹配
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null) {
            return false;
        }
        return auth.getAuthorities().stream()
                .anyMatch(granted -> granted.getAuthority().equals(permission));
    }

}
